package com.zlc.sykks.controller;

import com.zlc.sykks.CourseDesign.FileRunnable;
import com.zlc.sykks.result.Result;
import com.zlc.sykks.constance.FileConstance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/*
    22网络
* 2002208180141赵联城课程设计后端代码
* */
@RestController
@Slf4j
public class TestController {

    //线程池
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("files") MultipartFile files[]){
        log.info("服务器接收文件:{}",files);
        //1. 若文件为空，则报错
        if(files.length <= 0){
            return Result.error(FileConstance.FILE_IS_NOT_NULL);
        }

        for(MultipartFile file : files){
            //2.创建并调用线程
            FileRunnable fileRunnable = new FileRunnable();
            fileRunnable.setFile(file);
            threadPoolExecutor.execute(fileRunnable);
        }

        //3.上传文件成功，返回
        return Result.success(FileConstance.FILE_UPLOAD_SUCCESS);
    }
}

























